package com.xyzh.stj.web.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xyzh.stj.web.dto.right.QueryApplicationDTO;
import com.xyzh.stj.web.entity.ApplicationEntity;
import com.xyzh.stj.web.dao.ApplicationMapper;
import com.xyzh.stj.web.entity.OrganizationEntity;
import com.xyzh.stj.web.entity.RoleEntity;
import com.xyzh.stj.web.service.ApplicationService;
import com.xyzh.stj.web.service.OrganizationService;
import com.xyzh.stj.web.service.RoleService;
import com.xyzh.application.base.service.BasicServiceImpl;
import com.xyzh.application.base.vo.AuthorUser;
import com.xyzh.application.common.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author @JZB
 * @since 2023-11-24
 */
@Service
public class ApplicationServiceImpl extends BasicServiceImpl<ApplicationMapper, ApplicationEntity> implements ApplicationService {

    @Value("#{applicationProperies.info.adminCode}")
    private String sysAdmin;

    @Autowired
    private RoleService roleService;
    @Autowired
    private OrganizationService organizationService;

    @Override
    public boolean saveEntity(ApplicationEntity applicationEntity) {
        long sortkey = this.getBaseMapper().getMaxSortkey();
        applicationEntity.setSortkey(sortkey);
        return super.saveEntity(applicationEntity);
    }

    @Override
    public List<ApplicationEntity> getAllList() {
        return this.list();
    }

    @Override
    public List<ApplicationEntity> getUserApplications() {
        AuthorUser user = SecurityUtils.getAuthorUser();

        if (sysAdmin.equals(user.getUsername())) {
            return this.list();
        } else {
            List<RoleEntity> roles = roleService.getRoleByUserid(user.getId());
            List<ApplicationEntity> appsByRoles = this.getBaseMapper().getAppsByRoles(roles);

            List<OrganizationEntity> organizationEntities = organizationService.getOrganizations(user.getId());
            List<String> orgs = organizationService.getSubOrgidsByParent(organizationEntities);

            if(CollUtil.isNotEmpty(orgs)) {
                List<ApplicationEntity> orgApps = this.getBaseMapper().getAppsByOrgs(orgs);
                if (CollUtil.isNotEmpty(orgApps)) {
                    appsByRoles.addAll(orgApps);
                }
            }
            return appsByRoles.stream().distinct().sorted(Comparator.comparing(ApplicationEntity::getSortkey)).collect(Collectors.toList());

        }
    }

    @Override
    public List<ApplicationEntity> getApplicationsByRole(String roleId) {
        return this.getBaseMapper().getAppsByOrgid(roleId);
    }

    @Override
    public List<ApplicationEntity> getApplicationsByOrgs(String orgid) {
        return this.getBaseMapper().getAppsByOrgid(orgid);
    }

    @Override
    public List<ApplicationEntity> queryApplicationByUser(QueryApplicationDTO queryApplicationDTO){
        AuthorUser user = SecurityUtils.getAuthorUser();
        if (sysAdmin.equals(user.getUsername())) {
            LambdaQueryWrapper<ApplicationEntity> query=new LambdaQueryWrapper<>();
            query.eq(StrUtil.isNotEmpty(queryApplicationDTO.getStatus()),ApplicationEntity::getStatus,queryApplicationDTO.getStatus());
            query.like(StrUtil.isNotEmpty(queryApplicationDTO.getName()),ApplicationEntity::getApplicationName,queryApplicationDTO.getName());

            return this.list(query);
        } else {
            List<RoleEntity> roles = roleService.getRoleByUserid(user.getId());
            List<ApplicationEntity> appsByRoles = new ArrayList<>();
            if(CollUtil.isNotEmpty(roles)) {
                appsByRoles = this.getBaseMapper().queryAppsByRoles(roles, queryApplicationDTO);
            }
            List<OrganizationEntity> organizationEntities = organizationService.getOrganizations(user.getId());
            List<String> orgs = organizationService.getSubOrgidsByParent(organizationEntities);

            if(CollUtil.isNotEmpty(orgs)) {
                List<ApplicationEntity> orgApps = this.getBaseMapper().queryAppsByOrgs(orgs, queryApplicationDTO);
                if (CollUtil.isNotEmpty(orgApps)) {
                    appsByRoles.addAll(orgApps);
                }
            }
            return appsByRoles.stream().distinct().sorted(Comparator.comparing(ApplicationEntity::getSortkey)).collect(Collectors.toList());

        }
    }


}
